package com.example.util;

public class DataSourceContextHolder {
    //存储当前线程的副本变量值。作用：每个线程执行的都会是默认数据源，设置当前线程的数据源不会影响其他线程。
    private static final ThreadLocal<String> contextHolder = new ThreadLocal<String>();
    private static final String READ = "dataSource1";	//读库数据源名字，跟applicationContext-dao.xml中设置的名字要一致，用于切换数据源
    private static final String WRITE = "dataSource2";  //写库数据源

    public static void setRead() {
        contextHolder.set(READ);
    }

    public static void setWrite() {
        contextHolder.set(WRITE);
    }

    public static String getDbType() {
        //获取当前线程变量值
        return ((String) contextHolder.get());
    }

    public static void clearDbType() {
        contextHolder.remove();
    }
}
